<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>公告编辑</title>
    <link
      href="https://unpkg.com/@wangeditor/editor@latest/dist/css/style.css"
      rel="stylesheet"
    />
    <style>
      #editor—wrapper {
        border: 1px solid #ccc;
        z-index: 100; /* 按需定义 */
      }
      #toolbar-container {
        border-bottom: 1px solid #ccc;
      }
      #editor-container {
        height: 500px;
      }
      #gonggao {
        text-align: center;
      }
      #biaoti {
        font-weight: 600;
        display: inline-block;
      }
      button {
        float: right;
        margin-top: 20px;
        background-color: #ede35a;
        cursor: pointer;
      }
    </style>
  </head>
  <body>
    <h1 id="gonggao">公告编辑</h1>
    <p id="biaoti">标题:</p>
    <input type="text" id="inp" /><button>发布公告</button>
    <div id="editor—wrapper">
      <div id="toolbar-container"><!-- 工具栏 --></div>
      <div id="editor-container"><!-- 编辑器 --></div>
    </div>
    <script src="https://unpkg.com/@wangeditor/editor@latest/dist/index.js"></script>
    <script>
      const { createEditor, createToolbar } = window.wangEditor;

      const editorConfig = {
        placeholder: "请在这里输入内容...",
        onChange(editor) {
          const html = editor.getHtml();
          console.log("editor content", html);
          // 也可以同步到 <textarea>
        },
      };

      const editor = createEditor({
        selector: "#editor-container",
        html: "<p><br></p>",
        config: editorConfig,
        mode: "default", // or 'simple'
      });

      const toolbarConfig = {};

      const toolbar = createToolbar({
        editor,
        selector: "#toolbar-container",
        config: toolbarConfig,
        mode: "default", // or 'simple'
      });
      let btn = document.querySelector("button");
      let tit = document.querySelector("#inp");
      btn.addEventListener("click", function () {
        let html = editor.getHtml();
        let sTit = tit.value;
        localStorage.setItem(
          "notice",
          JSON.stringify({ titl: sTit, content: html })
        );
        alert("发布成功");
        window.location.href = "home.html";
      });
    </script>
  </body>
</html>
